package com.demo.servicelmpl;

import com.demo.bean.ConflictObjects;
import com.demo.bean.FourNums;
import com.demo.bean.Lesson;
import com.demo.mapper.LessonMapper;
import com.demo.service.LessonService;
import com.demo.service.RedisService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedHashMap;

/**
 * @author 李天航
 * 每周活动
 */
@Service
public class LessonServiceImpl implements LessonService {

    @Resource
    LessonMapper lessonMapper;
    @Resource
    RedisService redisService;

    public static int MAX_LESSON_NUM =20;

    private ConflictObjects judgeConflict(Lesson lesson)
    {
        Lesson lesson1 =lessonMapper.firstLargerBeginTime(lesson);
        if(lesson1!=null&&lesson.getEndTime()!=null&& lesson1.getBeginTime()<lesson.getEndTime()) {
            return new ConflictObjects(false,lesson1,lesson);
        }
        Lesson lesson2=lessonMapper.firstLargerEndTime(lesson);
        if(lesson2!=null&&lesson.getBeginTime()!=null&&lesson2.getBeginTime()<lesson.getBeginTime()) {
            return new ConflictObjects(false,lesson2,lesson);
        }
        return new ConflictObjects(true,null,null);
    }

    @Override
    public LinkedHashMap<String, Object> addLesson(Lesson lesson) {
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        try {
            if(lesson.getBeginTime()>lesson.getEndTime())
            {
                resultMap.put("status",500);
                resultMap.put("msg","起始时间不能大于终止时间");
                return resultMap;
            }
            ConflictObjects conflictObjects=judgeConflict(lesson);
            if(!conflictObjects.isNotConflict)
            {
                resultMap.put("status",500);
                resultMap.put("msg","活动与其他活动冲突");
                resultMap.put("addLesson",conflictObjects.newObject);
                resultMap.put("conflictLesson",conflictObjects.rawObject);
            }
            else
            {
                if(lessonMapper.getLessonCount(lesson.getUserAccount())<MAX_LESSON_NUM) {
                    lessonMapper.addLesson(lesson);
                    resultMap.put("status", 200);
                    resultMap.put("msg", "添加成功");
                }
                else
                {
                    resultMap.put("status", 500);
                    resultMap.put("msg", "数量达到上限");
                }

            }
        }catch (Exception e)
        {
            e.printStackTrace();
            resultMap.put("status",500);
            resultMap.put("msg",e.getMessage());
        }
        return resultMap;
    }

    @Override
    public LinkedHashMap<String, Object> deleteLesson(Integer lessonId, String userAccount) {
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        try {
            Lesson lesson= lessonMapper.getLessonById(lessonId);
            if(!lesson.getUserAccount().equals(userAccount))
            {
                resultMap.put("status",500);
                resultMap.put("msg","无修改权限");
            }
            else {
                lessonMapper.deleteLesson(lessonId);
                resultMap.put("status", 200);
                resultMap.put("msg", "删除成功");
            }
        }catch (Exception e)
        {
            e.printStackTrace();
            resultMap.put("status",500);
            resultMap.put("msg",e.getMessage());
        }
        return resultMap;
    }

    @Override
    public LinkedHashMap<String, Object> updateLesson(Lesson lesson) {
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        try {
            if(lesson.getBeginTime()==null&&lesson.getEndTime()==null)
            {
                lessonMapper.updateLesson(lesson);
                resultMap.put("status", 200);
                resultMap.put("msg", "修改成功");
                return resultMap;
            }
            Lesson rawLesson=lessonMapper.getLessonById(lesson.getLessonId());
            if(lesson.getUserAccount().equals(rawLesson.getUserAccount())) {
                if(lesson.getBeginTime()!=null) {
                    rawLesson.setBeginTime(lesson.getBeginTime());
                }
                if(lesson.getEndTime()!=null) {
                    rawLesson.setEndTime(lesson.getEndTime());
                }
                if(rawLesson.getBeginTime()> rawLesson.getEndTime())
                {
                    resultMap.put("status",500);
                    resultMap.put("msg","起始时间不能大于终止时间");
                    return resultMap;
                }
                ConflictObjects conflictObjects=judgeConflict(rawLesson);
                if(conflictObjects.isNotConflict) {
                    lessonMapper.updateLesson(lesson);
                    resultMap.put("status", 200);
                    resultMap.put("msg", "修改成功");
                }
                else{
                    resultMap.put("status",500);
                    resultMap.put("msg","活动与其他活动冲突");
                    resultMap.put("updateLesson",conflictObjects.newObject);
                    resultMap.put("conflictLesson",conflictObjects.rawObject);
                }
            }
            else
            {
                resultMap.put("status", 500);
                resultMap.put("msg", "无修改权限");
            }
        }catch (Exception e)
        {
            e.printStackTrace();
            resultMap.put("status",500);
            resultMap.put("msg",e.getMessage());
        }
        return resultMap;
    }

    @Override
    public LinkedHashMap<String, Object> getLessonsByUserAccount(String userAccount) {
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        try {
            resultMap.put("lessons",lessonMapper.getLessonsByUserAccount(userAccount));
            resultMap.put("status",200);
            resultMap.put("msg","查看成功");
        }catch (Exception e)
        {
            e.printStackTrace();
            resultMap.put("status",500);
            resultMap.put("msg",e.getMessage());
        }
        return resultMap;
    }
}
